package org.hxzon.demo.jt.logsearcher.web.components;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.BindingConstants;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.annotations.AfterRender;
import org.apache.tapestry5.annotations.BeginRender;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.corelib.base.AbstractField;
import org.apache.tapestry5.internal.TapestryInternalUtils;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
import org.apache.tapestry5.services.Request;

public class SelectIndexNames extends AbstractField {
    @Parameter(name = "title", defaultPrefix = BindingConstants.LITERAL)
    private String _title;

    @Parameter(name = "value", required = true)
    private List<String> _value;//提交值

    @Parameter(name = "source", required = true, allowNull = false)
    private List<String> _source;//候选值

    @Parameter(name = "labelFirst", value = "false")
    private boolean _labelFirst;

    @Parameter(name = "class", defaultPrefix = BindingConstants.LITERAL)
    private String _itemClass;

    @Inject
    private Request _request;

    @Inject
    private ComponentResources _resources;

    @BeginRender
    void begin(MarkupWriter writer) {
        writeTitle(writer);
        //
        String clientId = getClientId();
        String ctrlName = getControlName();
        for (String value : _source) {
            writeCheckbox(writer, (clientId + "_" + value), ctrlName, false, value, value, _labelFirst, _itemClass);
        }// end for
    }

    private void writeCheckbox(MarkupWriter writer, String clientId, String ctrlName, boolean checked, String value, String label, boolean labelFirst, String itemClass) {
        //boolean drawItem = itemClass != null;
        boolean drawItem = true;
        if (drawItem) {
            writer.element("span", "class", buildItemClass(itemClass, checked));
        }
        if (labelFirst) {
            writer.element("label", "for", clientId);
            writer.write(label);
            writer.end();
        }
        writer.element("input", "type", "checkbox", "name", ctrlName, "id", clientId, "checked", checked ? "checked" : null, "value", value);
        writer.end();
        if (!labelFirst) {
            writer.element("label", "for", clientId);
            writer.write(label);
            writer.end();
        }
        if (drawItem) {
            writer.end();
        }
    }

    private String buildItemClass(String itemClass, boolean checked) {
        List<String> classes = CollectionFactory.newList();

        if (itemClass != null) {
            classes.add(itemClass);
        }
        return TapestryInternalUtils.toClassAttributeValue(classes);
    }

    protected void writeTitle(MarkupWriter writer) {
        if (_title != null) {
            writer.element("span", "class", "title");
            writer.write(_title);
            writer.end();
            writer.writeRaw("<br />");
        }
    }

    @AfterRender
    void after(MarkupWriter writer) {

    }

    @Override
    protected void processSubmission(String controlName) {

        String[] postedValues = _request.getParameters(controlName);
        if (postedValues == null || postedValues.length == 0) {
            if (_value != null) {
                _value.clear();
            }
            return;
        }
        List<String> result = new ArrayList<String>();
        for (String postedValue : postedValues) {
            result.add(postedValue);
        }
        if (_value == null) {
            _value = result;
        } else {
            _value.clear();
            _value.addAll(result);
        }
    }
}
